from feature_data.const_data import *
from feature_data.XmlData2DictData import Xml2Dict
from feature_data.answer_extration import Answer_extration
from nd_utils.networkx_util import *
from nd_utils.similarity_graph import Graph_similarity_matrix
from feature_data.Dict2Coauthor_Graphfeature import Feature_coauthor_extration
from model_metric import Model_metric

class Graph_coauthor_stage():
    def __init__(self, feature_coauthor_extration, answer_extration):
        self.feature_coauthor_extration = feature_coauthor_extration
        self.n_samples_dim_matrix = self.feature_coauthor_extration.paper_coauthor_cooccur_num_matrix

        self.init_graph = self.feature_coauthor_extration.init_graph
        self.graph_clustor = Graph_similarity_matrix(self.n_samples_dim_matrix)

        self.first_cluster_graph = self._get_cluster_graph()

        self.true_cluster_num = answer_extration.true_cluster_num
        self.true_paperidx_cluster_list = answer_extration.true_paperidx_cluster_list

        self.simulation_occur_flag = self._get_simulation_occur_flag()
        """
            in fact, this subgraph_list is cluster list
        """
        self.subgraph_list = self._get_subgraph_list()
        self.first_subgraph_nodes_list = self._get_first_subgraph_nodes_list()

    def _get_cluster_graph(self):
        return self.graph_clustor.first_stage_graph_main( deepcopy_graph(self.init_graph), threshold=0)

    def _get_simulation_occur_flag(self):
        predict_cluster_num = get_subgraph_num(self.first_cluster_graph)
        if predict_cluster_num >= self.true_cluster_num:
            return True
        else:
            return False

    def _get_subgraph_list(self):
        if self.simulation_occur_flag:
            return get_subgraphs(self.first_cluster_graph)
        else:
            return get_subgraphs(self.init_graph)

    def _get_first_subgraph_nodes_list(self):
        return get_subgraph_nodes_list(self.subgraph_list)

def print_graph_edges(first_stage_clust):
    edge_tuple_list = first_stage_clust.cluster_graph.edges()
    print len(edge_tuple_list)
    for edge_tuple in edge_tuple_list:
        print edge_tuple



def print_nonezero_num(similarity_matrix):
    sum = 0
    for i in range(similarity_matrix.shape[0] - 1):
        for j in range(i+1, similarity_matrix.shape[0]):
            if similarity_matrix[i][j] !=0:
                sum += 1
    print sum

def print_subgraphs(first_stage_clust):
    for subgraph in first_stage_clust.subgraph_list:
        print subgraph.nodes(), len(subgraph.nodes())


from nd_utils.trans_clusteridxlist_paperidx_pairset import get_paperidx_pairset_list
from nd_utils.str_util import get_name
from nd_utils.write_csv import write_csv_f
if __name__ == '__main__':
    new_csv_row_list = []

    for xml_file_name in xml_experiment_file_name_list:
        xml2dict = Xml2Dict(xml_dir + xml_file_name)  # 1.0 0.317204301075 0.481632653061
        feature_coauthor_extration = Feature_coauthor_extration(xml2dict)
        answer_extration = Answer_extration(xml2dict)
        first_stage_clust = Graph_coauthor_stage(feature_coauthor_extration, answer_extration)

        # print_graph_edges(first_stage_clust)

        # print_nonezero_num(first_stage_clust.n_samples_dim_matrix)
        # print first_stage_clust.simulation_occur_flag

        # from nd_utils.matplotlib_helper import *

        # plt_networkx_main_first( first_stage_clust.first_cluster_graph )

        # print_subgraphs(first_stage_clust)

        # paperid_pairset_list = get_paperidx_pairset_list(first_stage_clust.true_paperidx_cluster_list)
        #
        # predict_paperid_pairset_list = get_paperidx_pairset_list(first_stage_clust.first_subgraph_nodes_list)

        model_metric = Model_metric(first_stage_clust.true_paperidx_cluster_list, first_stage_clust.first_subgraph_nodes_list )
        new_csv_row = []
        new_csv_row.append(get_name(xml_file_name))
        new_csv_row.append(model_metric.pairwise_precision)
        new_csv_row.append(model_metric.pairwise_recall)
        new_csv_row.append(model_metric.pairwise_f1)
        print model_metric.pairwise_precision, model_metric.pairwise_recall, model_metric.pairwise_f1

        new_csv_row_list.append(new_csv_row)
    write_csv_f('first_stage_prf', new_csv_row_list, fileHeader1)
"""
True
0.981524249423 0.468061674009 0.633855331842
True
1.0 0.317204301075 0.481632653061
True
1.0 0.803069053708 0.890780141844
True
0.856994818653 0.692629815745 0.766095414544
True
1.0 0.773838630807 0.87250172295
True
0.982404692082 0.283657917019 0.440210249671
True
1.0 0.525672371638 0.689102564103
True
1.0 0.419962335217 0.59151193634
True
1.0 0.977375565611 0.988558352403
True
1.0 0.649103139013 0.787219578518
"""